<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="script/aes.js"></script>
<script type="text/javascript" src="script/base64.js"></script>
<script type="text/javascript" src="script/rsax.js"></script>

<script type="text/javascript" >
	
	
	
	//前后台传输
	function transport(){
		var obj = new Object();
		obj.name="我们都是东北人 ！";
		obj.age=17;
		obj.job="工人";
		
		var jsonString = JSON.stringify(obj);//将对象转换成json string
		alert(jsonString);
		
		var content = encodeURI(encodeURI(jsonString)); //必须两次，否则会出错
		alert(content)
		
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=afterTransport; //回掉函数不能有()
		xmlhttp.open("POST","myServlet.p?type=transport&content="+content,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.send();
		
		function afterTransport(){
			//alert(xmlhttp.readyState +"--"+ xmlhttp.status)
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				
				var resCont = JSON.parse(decodeURIComponent(xmlhttp.responseText)); //将string 转换成json对象
				
				alert(resCont.content);
			}
		}// afterTransport
		
	}//transport
	
	
	//base64 加密与解密
	function base64(){
		var content = "我们都是东北人 ！"
		var encryptedContent = Base64.encode(content);
		alert("密文 :"+encryptedContent);
		alert("解密 :"+Base64.decode(encryptedContent));
	}
	
	
	//rsa 解密
	function rsaEncrypt(){
		
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=rsaEncryptCallBack; //回掉函数不能有()
		xmlhttp.open("POST","myServlet.p?type=rsaencrypt&content="+"",true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.send();
		
		function rsaEncryptCallBack(){
			
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				
				var resCont = JSON.parse(decodeURIComponent(xmlhttp.responseText)); //将string 转换成json对象
				
				//alert("N : "+resCont.N);
				//alert("E : "+resCont.E);
				//alert("D : "+resCont.D);
				//alert("密文 : "+resCont.content);
				
				RSASetPrivate(resCont.N,resCont.E,resCont.D);
				alert(RSADecrypt(resCont.content));
			}
			
		}//callBack
	}//rsaEncrypt
	
	
	
	
	//rsa 加密,获取公钥
	function rsaInit(){
		
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=rsaDecryptCallBack; //回掉函数不能有()
		xmlhttp.open("POST","myServlet.p?type=rsagpk&content="+"",true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.send();
		
		function rsaDecryptCallBack(){
			
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				var resCont = JSON.parse(decodeURIComponent(xmlhttp.responseText)); //将string 转换成json对象
				//alert("N : "+resCont.N);
				//alert("E : "+resCont.E);
				
				RSASetPublic(resCont.N,resCont.E);
				var contentDecrypted = RSAEncrypt("我们都是东北人呢哈 @@@@@@@@@@@@");
				
				doEncrypt(contentDecrypted);
			}
		}//rsaDecryptCallBack
		
		
		function doEncrypt(contentDecrypted){
			
			xmlhttp=new XMLHttpRequest();
			xmlhttp.open("POST","myServlet.p?type=rsadecrypt&content="+encodeURI(encodeURI(contentDecrypted)),true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			xmlhttp.send();
		}
		
	}
	
	
	
	
	
	
	
	
</script>

<title>Insert title here</title>
</head>
<body>
	
	<div>
		<h2> welcome to myProject</h2>
		
		<button type="button" onclick="transport()">前后台传输</button>
		<button type="button" onclick="base64()">解密BASE64</button>
		<button type="button" onclick="rsaEncrypt()">解密RSA</button>
		<button type="button" onclick="rsaInit()">加密RSA</button>
		
		<h2> </h2>
	</div>
	
	
	<div>
		<audio width="320" height="60" controls="controls">
	        <source src="audio/music1.mp3" type="audio/mpeg">
	    Your browser does not support the video tag.
	    </audio>
	</div>
    
    <div>
	    <video width="720" height="480" controls="controls">
	        <source src="movie/movie2.mp4" type="video/mp4">
		Your browser does not support the video tag.
		</video>
    </div>
	
	
	
</body>
</html>